package com.lmu.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmu.travel.entity.*;
import com.lmu.travel.service.RouteImgService;
import com.lmu.travel.service.RouteService;
import com.lmu.travel.service.SellerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author liuxuhuang
 * @className: RouteController
 * @description: 路线接口
 * @email liuxuhuang3970@qq.com
 * @date 2021/6/28 20:24
 * @version：1.0
 */
@Api(value = "路线接口")
@RestController
@RequestMapping(value = "route")
public class RouteController {
    @Autowired
    private RouteService routeService;
    @Autowired
    private RouteImgService routeImgService;
    @Autowired
    private SellerService sellerService;

    @GetMapping(value = "getRoutePage/{current}/{size}/{cid}")
    @ApiOperation(value = "分页显示数据接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页", dataType = "int", required = true),
            @ApiImplicitParam(name = "size", value = "一页要显示多少条数据", dataType = "int", required = true),
            @ApiImplicitParam(name = "cid", value = "分类id", required = true, dataType = "int")})
    public Map<String, Object> getRoutePage(@PathVariable("current") Integer current, @PathVariable("size") Integer size, @PathVariable("cid") Integer cid) {
        Page page;
        if (current <= 0 || size <= 0) {
            page = new Page(1, 5);
        } else {
            page = new Page(current, size);
        }
        QueryWrapper queryWrapper = new QueryWrapper<Category>();
        queryWrapper.eq("cid", cid);
        IPage iPage = routeService.page(page, queryWrapper);
        List<Route> routes = routeService.list(queryWrapper);
        /*
        System.out.println("数据一个有多少条" + iPage.getTotal());
        System.out.println("第" + iPage.getCurrent() + "页数据：" + iPage.getRecords());
        System.out.println("总共多少页：" + iPage.getPages());
         */
        if (routes.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("total", iPage.getTotal());
            map.put("current", iPage.getCurrent());
            map.put("page", iPage.getPages());
            map.put("data", iPage.getRecords());
            Map<String, Object> map1 = new HashMap<>();
            map1.put("code", 200);
            map1.put("flag", true);
            map1.put("data", map);
            map1.put("msg", "查询成功");
            return map1;
        }
        Map<String, Object> map = new HashMap();
        map.put("code", 201);
        map.put("flag", false);
        map.put("data", "查无数据");
        map.put("msg", "查无数据");
        return map;
    }

    @GetMapping("findRouteOne/{rid}")
    @ApiOperation(value = "根据id查询路线")
    @ApiImplicitParam(name = "rid", value = "rid", dataType = "Integer", required = true)
    public Result<Route> findOne(@PathVariable("rid") Integer rid) {
        if (rid == null) {
            return new Result<>(201, false, null, "id为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper<Route>();
        queryWrapper.eq("rid", rid);
        /**
         * 1.根据id去route表查询route对象
         */
        Route route = routeService.getOne(queryWrapper);
        if (route == null) {
            return new Result<>(201, false, null, "查询数据为空");
        }
        /**
         * 2.根据route的id去查询图片集合信息
         */
        List<RouteImg> routeImgs = routeImgService.list(queryWrapper);
        route.setRouteImgList(routeImgs);
        /**
         * 3.根据route的sid(商家id)查询商家对象
         */
        Seller seller = sellerService.getById(route.getSid());
        route.setSeller(seller);
        /**
         * 4.获取天气信息
         */
        WeatherInfo weatherInfo = routeService.getWeatherInfo(route.getDistrictid());
        route.setWeatherInfo(weatherInfo);
        /**
         * 5.更新该路线的浏览次数
         */
        routeService.updateRouteCount(String.valueOf(rid));
        return new Result<Route>(200, true, route, "根据id查询路线成功");
    }

    /**
     * 金额，商家名称，商品编号,用户id,
     *
     * @return
     */
    @ApiOperation(value = "添加订单")
    @PostMapping(value = "addOrder/{money}/{seller}/{userid}/{route_id}")
    @ApiImplicitParams({@ApiImplicitParam(name = "money", value = "付款金额", required = true, dataType = "double"),
            @ApiImplicitParam(name = "seller", value = "商家名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userid", value = "用户id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "route_id", value = "路线id", required = true, dataType = "String")})
    public Result<Route> addOrder(@PathVariable("money") double money, @PathVariable("seller") String seller, @PathVariable("userid") String userid, @PathVariable("route_id") String route_id) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSS");
        String out_trade_no = sdf.format(date);

        return null;
    }

    @GetMapping("findHost")
    @ApiOperation("查询热度排前5的路线")
    public Result<Route> findHot() {
        List<Route> routes = routeService.selectHot();
        return new Result<>(201, true, routes, "查询热度路线成功");
    }
}
